package com.autumn.sms.client.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

import com.autumn.mq.event.IMqEventBus;
import com.autumn.sms.SmsEventData;
import com.autumn.sms.client.IAutumnSmsSendClient;
import com.autumn.sms.client.SmsSendMessage;
import com.autumn.sms.client.SmsVerificationCodeMessage;
import com.autumn.util.ExceptionUtils;
import com.autumn.util.NetworkUtils;
import com.autumn.util.StringUtils;
import com.autumn.util.TimeSpan;
import com.autumn.util.WebUtils;

/**
 * 短信发送客户端实现
 * 
 * @author 老码农
 *
 *         2018-01-11 12:33:23
 */
public class AutumnSmsSendClientImpl implements IAutumnSmsSendClient {

	private final IMqEventBus mqEventBus;

	/**
	 * 服务id
	 */
	private final String serviceId;

	private final String serviceKey;

	/**
	 * 
	 * @param mqEventBus
	 * @param serviceKey
	 * @param serviceId
	 */
	public AutumnSmsSendClientImpl(IMqEventBus mqEventBus, String serviceKey, String serviceId) {
		this.mqEventBus = mqEventBus;
		this.serviceId = serviceId;
		this.serviceKey = serviceKey;
		// 防止同一实例未注入
		mqEventBus.getFactory().register(null, new String[] { SmsEventData.class.getPackage().getName() });
	}

	private String serviceIp = null;

	private String getServiceIp() {
		if (serviceIp == null) {
			serviceIp = NetworkUtils.getHostAddress();
		}
		return serviceIp;
	}

	/**
	 * 创建事件数据
	 * 
	 * @param message
	 * @param isVerificationCode
	 * @param verificationCodeType
	 * @return
	 *
	 */
	private SmsEventData createEventData(SmsSendMessage message, boolean isVerificationCode,
			Integer verificationCodeType) {
		message.validate(isVerificationCode);
		if (StringUtils.isNullOrBlank(message.getMessageId())) {
			message.setMessageId(UUID.randomUUID().toString().replace("-", ""));
		}
		SmsEventData eventData = new SmsEventData();
		eventData.setServiceId(this.serviceId);
		eventData.setServiceKey(this.serviceKey);
		eventData.setMessageId(message.getMessageId());
		eventData.setIsVerificationCode(isVerificationCode);
		eventData.setEventTime(new Date());
		if (isVerificationCode) {
			eventData.setVerificationCodeType(verificationCodeType);
		} else {
			eventData.setVerificationCodeType(null);
		}
		eventData.setTemplateCode(message.getTemplateCode());
		eventData.setMobilePhone(message.getMobilePhone());
		eventData.setServiceIp(this.getServiceIp());
		eventData.setClientIp(WebUtils.getCurrentRequestAddress());
		if (message.getParams() != null) {
			eventData.setParams(new HashMap<>(message.getParams()));
		}
		return eventData;
	}

	@Override
	public void send(SmsSendMessage message) {
		send(message, null);
	}

	@Override
	public void send(SmsSendMessage message, TimeSpan delayTimeSpan) {
		if (delayTimeSpan != null) {
			send(message, delayTimeSpan.getTotalMilliseconds());
		} else {
			send(message, 0L);
		}
	}

	@Override
	public void send(SmsSendMessage message, long delayMilliseconds) {
		ExceptionUtils.checkNotNull(message, "message");
		SmsEventData eventData = createEventData(message, false, null);
		mqEventBus.send(eventData, message.getMessageId(), delayMilliseconds);
	}

	@Override
	public void sendVerificationCode(SmsVerificationCodeMessage message) {
		ExceptionUtils.checkNotNull(message, "message");
		SmsSendMessage msg = message.createSendMessage();
		SmsEventData eventData = createEventData(msg, true, message.getValidateCodeType());
		mqEventBus.send(eventData, message.getMessageId(), 0L);
	}

}
